package com.boang.json;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.boang.po.Cla;

import java.sql.Array;
import java.util.ArrayList;
import java.util.List;

/*
  @author: 13813
  @date: 2024/7/26 14:43
*/
public class JsonDemo {
    public static void main(String[] args) {
//        ObjectToJson();
        String jsonString = "{\"id\":1,\"name\":\"51期\",\"num\":33}";
        Cla cla = JSON.parseObject(jsonString, Cla.class);
        System.out.println(cla);

        String jsonString2 = "[{\"id\":1,\"name\":\"51期\",\"num\":33},{\"id\":2,\"name\":\"52期\",\"num\":20}]";
        List<Cla> list = JSONArray.parseArray(jsonString2, Cla.class);
        System.out.println(list);
    }

    private static void ObjectToJson() {
        //------------------对象-->JSON--------------------------------------------
        Cla cla = new Cla(1, "51期", 33);
//        String json = "{\"id\":1,\"name\":\"51期\",\"num\":33}";

        String jsonString = JSON.toJSONString(cla);
        System.out.println(jsonString);
        //{"id":1,"name":"51期","num":33}


        ArrayList<Cla> list = new ArrayList<>();
        list.add(new Cla(1, "51期", 33));
        list.add(new Cla(2, "52期", 20));

        String jsonString2 = JSONArray.toJSONString(list);
        System.out.println(jsonString2);
        //[{"id":1,"name":"51期","num":33},{"id":2,"name":"52期","num":20}]
    }
}
